package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.common.login.UserLogin;
import com.atguigu.tingshu.vo.album.AlbumTrackListVo;
import com.atguigu.tingshu.vo.album.TrackStatVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album")
@SuppressWarnings({"all"})
public class TrackInfoApiController {

	@Autowired
	private TrackInfoService trackInfoService;

	@Autowired
	private VodService vodService;

	@PostMapping("/trackInfo/uploadTrack")
	public Result<Map<String, String>> uploadTrack(MultipartFile file) {
		Map<String, String> map = vodService.uploadTrack(file);
		return Result.ok(map);
	}

	@PostMapping("/trackInfo/saveTrackInfo")
	public Result saveTrackInfo(@RequestBody @Valid TrackInfoVo trackInfoVo) {
		//用户id
		Long userId = AuthContextHolder.getUserId();
		trackInfoService.savaTrackInfo(trackInfoVo, userId);
		return Result.ok();
	}

	@PostMapping("/trackInfo/findUserTrackPage/{page}/{limit}")
	public Result<Page<TrackListVo>> getUserTrackByPage(@PathVariable int page, @PathVariable int limit, @RequestBody TrackInfoQuery trackInfoQuery) {
		//用户id
		Long userId = AuthContextHolder.getUserId();
		Page<TrackListVo> pageInfo = new Page<>(page, limit);
		pageInfo = trackInfoService.getUserTrackByPage(pageInfo, trackInfoQuery);
		return Result.ok(pageInfo);
	}

	@GetMapping("/trackInfo/getTrackInfo/{id}")
	public Result<TrackInfo> getTrackInfoById(@PathVariable Long id) {
		TrackInfo trackInfo = trackInfoService.getById(id);
		return Result.ok(trackInfo);
	}

	@PutMapping("/trackInfo/updateTrackInfo/{id}")
	public Result updateTrackInfo(@PathVariable Long id, @RequestBody @Validated TrackInfoVo trackInfoVo) {
		trackInfoService.updateTrackInfo(id, trackInfoVo);
		return Result.ok();
	}

	@GetMapping("/trackInfo/findAlbumTrackPage/{albumId}/{page}/{limit}")
	@UserLogin(required = false)
	public Result<Page<AlbumTrackListVo>> findAlbumTrackPage(@PathVariable Long albumId, @PathVariable Long page, @PathVariable Long limit) {
		Long userId = AuthContextHolder.getUserId();
		Page pageInfo = new Page<>(page, limit);
		pageInfo = trackInfoService.findAlbumTrackPage(pageInfo, albumId, userId);
		return Result.ok(pageInfo);
	}

	@GetMapping("/trackInfo/getTrackStatVo/{trackId}")
	public Result<TrackStatVo> getTrackStatVo(@PathVariable Long trackId) {
		TrackStatVo trackStatVo = trackInfoService.getTrackStatVo(trackId);
		return Result.ok(trackStatVo);
	}

	@UserLogin(required = false)
	@GetMapping("/trackInfo/findUserTrackPaidList/{trackId}")
	public Result<List<Map<String, Object>>> getUserWaitBuyTrackPayList(@PathVariable Long trackId) {
		Long userId = AuthContextHolder.getUserId();
		List<Map<String, Object>> list = trackInfoService.getUserWaitBuyTrackPayList(userId,trackId);
		return Result.ok(list);
	}

	/**
	 * 提供给订单服务渲染购买商品（声音）列表-查询当前用户待购买声音列表
	 *
	 * @param trackId    声音ID
	 * @param trackCount 数量
	 * @return
	 */
	@UserLogin
	@GetMapping("/trackInfo/findPaidTrackInfoList/{trackId}/{trackCount}")
	public Result<List<TrackInfo>> getWaitBuyTrackInfoList(@PathVariable Long trackId, @PathVariable int trackCount) {
		Long userId = AuthContextHolder.getUserId();
		List<TrackInfo> trackInfoList = trackInfoService.getWaitBuyTrackInfoList(userId, trackId, trackCount);
		return Result.ok(trackInfoList);
	}
}

